package com.zhihao.takeaway.controller;

import com.zhihao.takeaway.common.utils.*;
import com.zhihao.takeaway.entity.Dish;
import com.zhihao.takeaway.entity.Orders;
import com.zhihao.takeaway.entity.ShoppingCart;
import com.zhihao.takeaway.service.DishService;
import com.zhihao.takeaway.service.OrdersService;
import com.zhihao.takeaway.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private DishService dishService;

    @GetMapping("/getTableData")
    public Result getTableData(Integer currentPage,Integer size){
        String EmployeeId = getEmployeeId();
        Result result = new Result();
        if(EmployeeId != null && !EmployeeId.equals("")) {
            EasyUITable easyUITable = ordersService.getTableData(currentPage, size);
            if (easyUITable.getTotal() == 0) {
                result.fail("未查询到相关结果");
            } else {
                result.success("查询到" + easyUITable.getTotal() + "条信息");
                result.setData(easyUITable);
            }
        }else{
            result.fail("请检查是否登录");
        }

        return result;
    }

    @GetMapping("/search")
    public Result search(String id,String orderTime,Integer page){
        String EmployeeId = getEmployeeId();
        Result result = new Result();
        if(EmployeeId != null || EmployeeId.equals("")) {
//            Integer page1 = Integer.parseInt(page);
            EasyUITable easyUITable = ordersService.search(id,orderTime,page);
            result.success("查询到"+easyUITable.getTotal()+"条信息");
            result.setData(easyUITable);
        }else{
            result.fail("请检查是否登录");
        }
        return result;
    }
    // 直接点击了支付
    @PostMapping("/save")
    public Result save(@RequestBody Orders orders){
        Result result = new Result();
        String UserId = getUserId();
        if(UserId == null || UserId.equals("")){
            result.fail("当前用户未登录");
        }else{
            //
            Boolean flag = false;
            System.out.println(orders);
            if(orders.getId() == null||orders.getId().equals("")){
                orders.setUserId(UserId);
                orders.setStatus(2);
                orders.setOrderTime(DateTool.getCurrTime());
                if(orders.getShoppingCartId() != null){
                    shoppingCartService.cancel(orders.getShoppingCartId());
                }
                flag = ordersService.save(orders);
            }else{
                orders.setStatus(2);
                flag = ordersService.updateById(orders);
            }
            if(flag){
                result.success("成功下单");
            }else{
                result.fail("下单失败");
            }
        }
        return result;
    }
    @GetMapping("/getOrderDataById")
    public Result getOrderDataById(String distinguish){
        Result result = new Result();
        String UserId = getUserId();
        if(UserId == null || UserId.equals("")){
            result.fail("当前用户未登录");
        }else{
            EasyUITable easyUITable = ordersService.getOrderDataById(UserId,distinguish);
            if (easyUITable.getTotal() == 0){
                result.fail("未查询到相关内容");
            }else{
                result.setData(easyUITable);
                result.success("成功查询到"+ easyUITable.getTotal()+"条数据");

            }
        }
        return result;
    }
    @GetMapping("/getSuspenseOrderData")
    public Result getSuspenseOrderData(Integer currentPage){
        String EmployeeId = getEmployeeId();
        Result result = new Result();
        if(EmployeeId == null || EmployeeId.equals("")) {
            result.fail("当前员工未登录");
        }else{
            EasyUITable easyUITable = ordersService.getOrdersByCondition((currentPage-1)*6,null,2,6,null);
            result.setData(easyUITable);
            result.success("获取成功");
        }
        return result;
    }
    @GetMapping("/getShippedOrderData")
    public Result getShippedOrderData(){
        String EmployeeId = getEmployeeId();
        Result result = new Result();
        if(EmployeeId == null || EmployeeId.equals("")) {
            result.fail("当前员工未登录");
        }else{
            EasyUITable easyUITable = ordersService.getOrdersByCondition(null,EmployeeId,3,null,null);
            if(easyUITable.getTotal() == 0){
                result.fail("当前员工未抢到订单");
            }else{
                result.setData(easyUITable);
                result.success("当前正在配送的订单为"+easyUITable.getTotal()+"个，请及时配送");
            }
        }
        return result;
    }

    @GetMapping("/getCompletedOrderData")
    public Result getCompletedOrderData(Integer currentPage){
        String EmployeeId = getEmployeeId();
        Result result = new Result();
        if(EmployeeId == null || EmployeeId.equals("")) {
            result.fail("当前员工未登录");
        }else{
            EasyUITable easyUITable = ordersService.getOrdersByCondition((currentPage-1)*6,EmployeeId,4,6,null);
            result.setData(easyUITable);
            result.success("当前已配送的订单为"+easyUITable.getTotal()+"个");
        }
        return result;
    }

    @PostMapping("/delivery")
    public Result delivery(String id){
        String EmployeeId = getEmployeeId();
        Result result = new Result();
        if(EmployeeId == null || EmployeeId.equals("")) {
            result.fail("当前员工未登录");
        }else{
            Boolean flag = ordersService.delivery(id,EmployeeId);
            if(flag){
                result.success("抢单成功");
            }else{
                result.fail("抢单失败");
            }
        }
        return result;
    }
    @PostMapping("/completedOrderSearch")
    public Result completedOrderSearch(String id,Integer currentPage){
        String EmployeeId = getEmployeeId();
        Result result = new Result();
        if(EmployeeId == null || EmployeeId.equals("")) {
            result.fail("当前员工未登录");
        }else{
            if(id.equals("")){
                id = null;
            }
            EasyUITable easyUITable = ordersService.getOrdersByCondition((currentPage-1)*6,EmployeeId,4,6,id);
            result.setData(easyUITable);
            result.success("查询成功");
        }
        return result;
    }
    @PostMapping("/selectByMouth")
    public Result selectByMouth(Integer currentPage){
        String EmployeeId = getEmployeeId();
        Result result = new Result();
        if(EmployeeId == null || EmployeeId.equals("")) {
            result.fail("当前员工未登录");
        }else{
            String date = DateTool.getCurrMon();
            EasyUITable easyUITable = ordersService.getOrdersByConditionWithTime((currentPage-1)*6,EmployeeId,4,6,date);
            result.setData(easyUITable);
            result.success("成功查询到当月完成的订单");
        }
        return result;
    }
//    获取未完成订单的客户信息
    @GetMapping("/getSuspenseOrderUser")
    public Result getSuspenseOrderUser(){
        String EmployeeId = getEmployeeId();
        Result result = new Result();
        if(EmployeeId == null || EmployeeId.equals("")) {
            result.fail("当前员工未登录");
        }else{
           EasyUITable easyUITable = ordersService.getSuspenseOrderUser(EmployeeId);
            result.setData(easyUITable);
            result.success("成功查询");
        }
        return result;
    }
//    将订单状态改为   5
    @GetMapping("/toCancelOrder")
    public Result cancelById(String id){
        String UserId = getUserId();
        Result result = new Result();
        if(UserId == null || UserId.equals("")){
            result.fail("当前用户未登录，请及时登录");
        }else{
            if(ordersService.cancelById(id)) {
                result.success("取消成功");
            }else{
                result.fail("取消失败");
            }
        }
        return result;
    }
    @PostMapping("/toCancelPay")
    public Result toCancelPay(@RequestBody Orders orders){
        String UserId = getUserId();
        Result result = new Result();
        if(UserId == null || UserId.equals("")){
            result.fail("当前用户未登录，请及时登录");
        }else{
//            判断当前订单是否存在订单数据库中
            if(orders.getId() == null || orders.getId().equals("")){
                orders.setUserId(UserId);
                orders.setStatus(1);
                if(orders.getShoppingCartId() != null){
                    shoppingCartService.cancel(orders.getShoppingCartId());
                }
                Boolean flag = ordersService.save(orders);
                if(flag){
                    result.success("取消支付成功，订单保存在待支付订单中");
                }else{
                    result.fail("取消失败，请重新操作");
                }
            }
        }
        return result;
    }
    @PostMapping("/update")
    public Result update(@RequestBody Orders orders){
        String EmployeeId = getEmployeeId();
        Result result = new Result();
        if(EmployeeId == null || EmployeeId.equals("")) {
            result.fail("当前员工未登录");
        }else{
            orders.setCompleteTime(DateTool.getCurrTime());
            boolean b = ordersService.updateById(orders);
            if(b){
                result.success("更新成功");
            }else{
                result.fail("更新失败，请重试");
            }
        }
        return result;
    }

    public String getEmployeeId(){
        String token = request.getHeader("token");
        String EmployeeId = JwtUtil.validateToken(token);
        return EmployeeId;
    }
    public String getUserId(){
        String token = request.getHeader("token");
        String UserId = JwtUtil.validateToken(token);
        return UserId;
    }
}
